<!doctype html>
<html lang="zh-CN" th:replace="admin-layout :: layout(~{::title}, ~{}, ~{::script}, ~{::body}, 'label-list')"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <title>标签列表</title>
    <script>
        var $table = $("#table");
        var $remove = $("#removeBtn");
        var $update = $("#updateBtn");
        var $search = $("#searchBtn");
        var selections = []

        //$(this).serialize()
        $(function () {
            tableList(columns)
        })

        var columns = [{
            checkbox: true
        }, {
            field: 'name',
            title: '标签名称',
        }, {
            field: 'dataType',
            title: '标签类型',
            formatter: dataTypeFormatter
        }]

        function dataTypeFormatter(value, row, index) {
            if (value === 0) {
                return "试题";
            }
        }

        function beginTimeFormatter(value, row, index) {
            return formatDateTime(row.beginTime);
        }

        function endTimeFormatter(value, row, index) {
            return formatDateTime(row.endTime);
        }

        $remove.click(function () {
            confirmAlert(function () {
                var ids = getIdSelections();
                ajaxJson("/admin/label/ajax/delete", {"ids": ids.toString()}, function (res) {
                    if (res.status) {
                        $table.bootstrapTable('refresh')
                        $remove.prop('disabled', true);
                        $update.prop('disabled', true);
                    }
                })
            })
        })

        $update.click(function () {
            var ids = getIdSelections()
            if (ids){
                location.href = "/admin/label/save?id=" + ids[0]
            }
        })

        function getIdSelections() {
            return $.map($table.bootstrapTable('getSelections'), function (row) {
                return row.id
            })
        }

        $table.on('check.bs.table uncheck.bs.table ' +
            'check-all.bs.table uncheck-all.bs.table',
            function () {
                $remove.prop('disabled', !$table.bootstrapTable('getSelections').length);
                $update.prop('disabled', !$table.bootstrapTable('getSelections').length);

                // save your data, here just save the current page
                selections = getIdSelections()
                // push or splice the selections if you want to save all data selections
            })

        $search.click(function () {
            $("input[name='labelNumber']").val(1);
            $table.bootstrapTable('refresh')
        })
    </script>
</head>
<body>
<ol class="breadcrumb">
    <li class="active">标签列表</li>
</ol>
<div class="fyl-mb10">
    <form id="toolbar">
        <div class="row">
            <div class="col-sm-4 col-md-4 col-lg-2 fyl-mb10">
                <input type="text" class="form-control fyl-clear" name="name"
                       placeholder="标签名称">
            </div>
            <div class="col-sm-4 col-md-4 col-lg-2 fyl-mb10">
                <select class="form-control fyl-clear" name="dataType">
                    <option value="-1">标签类型</option>
                    <option value="0">试题</option>
                </select>
            </div>
            <div class="col-sm-4 col-md-4 col-lg-2 fyl-mb10">
                <a href="javascript:void(0)" class="btn btn-primary" id="searchBtn">查询</a>
                <a href="javascript:void(0)" class="btn btn-warning" id="clearSearchBtn">清空</a>
            </div>
        </div>
    </form>
</div>
<div class="fyl-mb20">
    <a th:href="@{/admin/label/save}" class="btn btn-primary">添加</a>
    <button type="button" class="btn btn-primary" id="updateBtn" disabled>修改</button>
    <button type="button" class="btn btn-danger" id="removeBtn" disabled>删除</button>
</div>
<table id="table" data-url="/admin/label/ajax/list"></table>
</body>
</html>